//
//  ZYCProfileParentApplyViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/21.
//

#import "ZYCProfileParentApplyViewController.h"
#import "ZYCProfileParentApplyCell.h"
#import "ZYCProfileParentApplyModel.h"
#import "ZYCNoDataView.h"

static NSInteger pageSize = 10;
@interface ZYCProfileParentApplyViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCProfileParentApplyCellDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, strong) ZYCNoDataView *noDataView;
@end

@implementation ZYCProfileParentApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的报名";
    [self.view addSubview:self.tableView];
    [self loadNewData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCProfileParentApplyCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCProfileParentApplyCell class]) forIndexPath:indexPath];
    [cell setSectionCircularTableView:tableView withIndexPath:indexPath];
    cell.delegate = self;
    cell.model = self.dataSource[indexPath.section];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 162.0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

// 列表数据
- (void)loadNewData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"studentId":self.studentId,
    };
    [MrjSVProgressHUD showWithStatus:@"加载中"];
    [[URLManager sharedURLManager] getUrlString:Appliance_Get_ParentRegistrationList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCProfileParentApplyModel class] json:responseObject[@"body"][@"list"]];
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:array];
                [self.tableView reloadData];
                // 每次下拉刷新需要重置一下
                [self reset];
                [self.tableView.mj_header endRefreshing];
                if (self.dataSource.count == 0) {
                    self.noDataView.hidden = NO;
                    self.tableView.hidden = YES;
                }else{
                    self.noDataView.hidden = YES;
                    self.tableView.hidden = NO;
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            [self.tableView.mj_header endRefreshing];
        }];
    
    
}

- (void)loadMoreData{
    self.pageNum = 1;
    NSDictionary *params = @{
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize),
        @"studentId":self.studentId,
    };
    [MrjSVProgressHUD showWithStatus:@"加载中"];
    [[URLManager sharedURLManager] getUrlString:Appliance_Get_ParentRegistrationList requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCProfileParentApplyModel class] json:responseObject[@"body"][@"list"]];
                if (array.count == 0) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.dataSource addObjectsFromArray:array];
                    [self.tableView reloadData];
                    [self.tableView.mj_footer endRefreshing];
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            [self.tableView.mj_footer endRefreshing];
        }];
}

- (void)reset{
    [self.tableView.mj_footer setRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_footer resetNoMoreData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYCProfileParentApplyModel *model = self.dataSource[indexPath.section];
    
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"取消报名" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"暂不" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self cancelEnrollingWithClassPlanId:model.classPlanId];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];


}


#pragma mark - ZYCProfileParentApplyCellDelegate



- (void)cancelEnrollingWithClassPlanId:(NSString *)classPlanId{
    NSDictionary *params = @{
        @"studentId":self.studentId,
        @"classPlanId":classPlanId,
    };
    
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_CancelEnroll requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                [MrjSVProgressHUD showSuccessWithStatus:responseObject[@"message"]];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}


- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight -kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCProfileParentApplyCell class] forCellReuseIdentifier:NSStringFromClass([ZYCProfileParentApplyCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
